# -*-coding:utf-8 -*-
"""
# File       : main_driver.py
# Time       ：2022/3/21 11:05
# Author     ：Coco
# version    ：python 3.7
"""
import pathlib
import sys
from PyQt5.QtCore import QSize, Qt, QRect, pyqtSignal, pyqtSlot
from PyQt5.QtGui import QIcon, QBrush, QColor, QPen, QFont, QPainter
from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox, QListWidgetItem, QHBoxLayout, QLabel, QPushButton, \
    QComboBox, QDialog, QVBoxLayout, QGridLayout, QLineEdit, QFormLayout, QFileDialog
from gui.custom_widget.QSwitchButton import QSwitchButton
from gui.custom_widget.DCPDialog import DCPDialog
from utils.parse_txt_util import *
from gui.main_gui import Gui


TEST_MODE = ["IP", "Iλ", "IV", "PV", "OE_TE_Alignment", "OO_Dembedding", "OO_TE_Alignment", "Spectral_Scan"]


class Driver(Gui):
    def __init__(self):
        super(Driver, self).__init__()
        self.subdie_list = None
        self._bind_actions()
        # all_sub_die_info_dict字典中 键为 SUB DIE 值为 列表，内含测试模式相应设置及 14个DCP的信息
        self.all_sub_die_info_dict = {}
        # self.generate_test_items()

    def _bind_actions(self):
        self.tb_pair_smu_dcp.clicked.connect(self._show_dcp_wd)
        self.tb_save_config.clicked.connect(self.save_file)
        self.tb_read_config.clicked.connect(self.read_config)
        self.tb_start.clicked.connect(self.start_measure)
        self.tb_stop.clicked.connect(self.stop_measure)
        self.tb_pause.clicked.connect(self.pause_measure)
        self.tb_continue.clicked.connect(self.continue_measure)
        self.tb_import_subdie_file.clicked.connect(self.import_subdie_file)
        self.pb_spp_proj.clicked.connect(lambda: self.open_folder_file_path(1))
        self.pb_map_path.clicked.connect(lambda: self.open_folder_file_path(2))
        self.pb_file_folder.clicked.connect(lambda: self.open_folder_file_path(3))

    def import_subdie_file(self):
        fileName, _ = QFileDialog.getOpenFileName(self, "导入文件", "./")
        if fileName:
            if pathlib.Path(fileName).exists():
                result_dict = parse_txt_to_dict(fileName)
                if result_dict:
                    self.subdie_list = result_dict.keys()
                    self.generate_test_items(self.subdie_list)
                    # self.update()
        else:
            self.show_critical("请选择文件")

    def open_folder_file_path(self, value):
        file_path = self.set_save_folder()
        if file_path:
            if value == 1:
                self.le_spp_proj.setText(file_path)
            elif value == 2:
                self.le_map_path.setText(file_path)
            else:
                self.le_file_folder.setText(file_path)
        else:
            self.show_critical("请选择文件保存的文件夹")

    def set_save_folder(self):
        prefix_path = QFileDialog.getExistingDirectory(self, "请选择选中的文件夹路径", "\\")
        if prefix_path:
            return prefix_path
        else:

            return None

    def continue_measure(self):
        self.show_info("continue_measure")

    def pause_measure(self):
        self.show_info("pause_measure")

    def stop_measure(self):
        self.show_info("stop_measure")

    def start_measure(self):
        self.show_info("start_measure")

    # 创建第一行表头  ListWidget 的第一行
    @staticmethod
    def create_title():
        wd_label = QWidget()
        wd_label.setFixedWidth(600)
        # wd_label.setStyleSheet("QWidget {border: 1px solid red}")
        hl_sub_die = QHBoxLayout(wd_label)
        hl_sub_die.setContentsMargins(10, 0, 20, 0)
        hl_sub_die.setSpacing(40)
        lb_sub_die_id = QLabel("SubDie ID".ljust(20))
        lb_test_proj = QLabel("测试工程")
        lb_proj_setup = QLabel("工程Setup")
        # lb_dcp_define = QLabel("DCP探针定义")
        hl_sub_die.addWidget(lb_sub_die_id)
        hl_sub_die.addWidget(lb_test_proj)
        hl_sub_die.addWidget(lb_proj_setup)
        # hl_sub_die.addWidget(lb_dcp_define)
        return wd_label

    # 创建每个 SubDie 行，是在 ListWidget 中的每一行
    def create_widget(self, SUB_DIE_LIST=None):
        wd_sub_die = QWidget()
        # wd_sub_die.setStyleSheet("QWidget {border: 1px solid red}")
        wd_sub_die.setFixedWidth(600)
        hl_sub_die = QHBoxLayout(wd_sub_die)
        hl_sub_die.setContentsMargins(10, 0, 20, 0)
        hl_sub_die.setSpacing(50)
        cb_seq_name = QComboBox()
        cb_seq_name.setObjectName("cb_seq_name")
        if SUB_DIE_LIST:
            cb_seq_name.addItems(SUB_DIE_LIST)
        # 测试工程
        cb_test_project = QComboBox()
        cb_test_project.addItems(TEST_MODE)
        # 工程 Setup
        pb_proj_setup = QPushButton("TEST")
        pb_proj_setup.clicked.connect(self._show_test_mode_wd)
        # DCP 探针定义
        # pb_dcp_define = QPushButton("TEST")
        # pb_dcp_define.clicked.connect(self._show_dcp_wd)
        hl_sub_die.addWidget(cb_seq_name)
        hl_sub_die.addWidget(cb_test_project)
        hl_sub_die.addWidget(pb_proj_setup)
        # hl_sub_die.addWidget(pb_dcp_define)
        cb_test_project.currentIndexChanged.connect(self.mode_changed_slot)
        # cb_seq_name.currentIndexChanged.connect(self.mode_changed_slot)
        return wd_sub_die

    # 切换 模式 槽函数
    def mode_changed_slot(self, index):
        sender = self.sender()
        if index >= 5:
            sender.parent().children()[3].setEnabled(False)
        else:
            sender.parent().children()[3].setEnabled(True)

    # 按列表生成全部 Sub Die
    def generate_test_items(self, subdie_list):
        # 生成表头
        self.lw_test_area.clear()
        new_widget = self.create_title()
        list_item = QListWidgetItem()
        list_item.setSizeHint(QSize(50, 40))
        list_item.setBackground(QBrush(QColor("#d9d9d9")))
        self.lw_test_area.addItem(list_item)
        self.lw_test_area.setItemWidget(list_item, new_widget)
        # 生成的控件添加到每一行
        for sub_die in subdie_list:
            new_widget = self.create_widget(subdie_list)
            list_item = QListWidgetItem()
            list_item.setSizeHint(QSize(50, 40))
            self.lw_test_area.addItem(list_item)
            self.lw_test_area.setItemWidget(list_item, new_widget)
            self.all_sub_die_info_dict[sub_die.strip()] = [[{} for _ in range(len(TEST_MODE))],
                                                           [i * 0 for i in range(14)]]

    # 按照选择的测试模式，返回对应的设置 QWidget
    def reflect_test_widget(self, index):
        setting_info = self.current_sub_die_info[0][index]  # 拿到对应的设置信息字典
        print(setting_info)
        # 生成一个所有控件的列表，根据 index 返回具体索引控件
        if index == 0:
            # IP 测试对应界面
            wd_ip = QWidget()
            hl_ip = QHBoxLayout(wd_ip)
            hl_ip.setSpacing(50)
            wd_left = QWidget()
            fl_left = QFormLayout(wd_left)
            fl_left.setHorizontalSpacing(25)
            fl_left.setVerticalSpacing(35)
            wd_right = QWidget()
            fl_right = QFormLayout(wd_right)
            fl_right.setHorizontalSpacing(25)
            fl_right.setVerticalSpacing(35)
            hl_ip.addWidget(wd_left)
            hl_ip.addWidget(wd_right)
            lb_u2722a = QLabel("U2722A Settings")

            lb_voltage_value = QLabel("Voltage Value：")
            wd_voltage_value = QWidget()
            hl_voltage_value = QHBoxLayout(wd_voltage_value)
            hl_voltage_value.setContentsMargins(0, 0, 0, 0)
            le_voltage_value = QLineEdit()
            le_voltage_value.setFixedWidth(100)
            cb_voltage_value = QComboBox()
            cb_voltage_value.setFixedWidth(60)
            cb_voltage_value.addItems(["mV", "V"])
            hl_voltage_value.addWidget(le_voltage_value, alignment=Qt.AlignLeft)
            hl_voltage_value.addWidget(cb_voltage_value)

            lb_voltage_range = QLabel("Voltage Range：")
            cb_voltage_range = QComboBox()
            cb_voltage_range.setFixedWidth(100)
            cb_voltage_range.addItems(["2V", "20V"])

            lb_current_nplc = QLabel("Current Nplc：")
            le_current_nplc = QLineEdit()
            le_current_nplc.setFixedWidth(100)

            lb_current_range = QLabel("Current Range：")
            cb_current_range = QComboBox()
            cb_current_range.setFixedWidth(100)
            cb_current_range.addItems(["1uA", "10uA", "100uA", "1mA", "10mA", "120mA", ])

            lb_current_limit = QLabel("Current Limit：")
            wd_current_limit = QWidget()
            hl_current_limit = QHBoxLayout(wd_current_limit)
            hl_current_limit.setContentsMargins(0, 0, 0, 0)
            le_current_limit = QLineEdit()
            le_current_limit.setFixedWidth(100)
            cb_current_limit = QComboBox()
            cb_current_limit.setFixedWidth(60)
            cb_current_limit.addItems(["uA", "mA", "A"])
            hl_current_limit.addWidget(le_current_limit, alignment=Qt.AlignLeft)
            hl_current_limit.addWidget(cb_current_limit)

            lb_output_state = QLabel("Output State：")
            pb_switch = QSwitchButton()

            lb_channel_num = QLabel("Channel Number：")
            cb_channel_num = QComboBox()
            cb_channel_num.setFixedWidth(130)
            cb_channel_num.addItems(
                ["Channel 1", "Channel 2", "Channel 3", "Channel 4", "Channel 5", "Channel 6", "Channel 7", "Channel 8",
                 "Channel 9", "Channel 10", ])

            lb_l8164b = QLabel("L8164B Settings")

            lb_wave_len = QLabel("Wave Length：")
            wd_wave_len = QWidget()
            hl_wave_len = QHBoxLayout(wd_wave_len)
            hl_wave_len.setContentsMargins(0, 0, 0, 0)
            le_wave_len = QLineEdit()
            le_wave_len.setFixedWidth(100)
            cb_wave_len = QComboBox()
            cb_wave_len.setFixedWidth(60)
            cb_wave_len.addItem("nm")
            hl_wave_len.addWidget(le_wave_len, alignment=Qt.AlignLeft)
            hl_wave_len.addWidget(cb_wave_len)

            lb_power_unit = QLabel("Power Unit：")
            cb_power_unit = QComboBox()
            cb_power_unit.setFixedWidth(100)
            cb_power_unit.addItems(["dBm", "W"])

            lb_stable_schedule = QLabel("Stable Schedule：")
            le_stable_schedule = QLineEdit()
            le_stable_schedule.setFixedWidth(100)

            wd_blank = QWidget()
            hl_blank = QHBoxLayout(wd_blank)
            hl_blank.setContentsMargins(0, 0, 0, 0)
            lb_blank = QLabel("")
            hl_blank.addWidget(lb_blank)

            pb_submit = QPushButton("保存设置")
            pb_submit.clicked.connect(self.save_ip_setting)
            pb_submit.setFixedWidth(100)

            fl_left.addRow(lb_u2722a)
            fl_left.addRow(lb_voltage_value, wd_voltage_value)
            fl_left.addRow(lb_voltage_range, cb_voltage_range)
            fl_left.addRow(lb_current_nplc, le_current_nplc)
            fl_left.addRow(lb_current_range, cb_current_range)
            fl_left.addRow(lb_current_limit, wd_current_limit)
            fl_left.addRow(lb_output_state, pb_switch)
            fl_right.addRow(lb_l8164b)
            fl_right.addRow(lb_wave_len, wd_wave_len)
            fl_right.addRow(lb_power_unit, cb_power_unit)
            fl_right.addRow(lb_stable_schedule, le_stable_schedule)
            fl_right.addRow(QLabel(), wd_blank)
            fl_right.addRow(lb_channel_num, cb_channel_num)
            fl_right.addRow(QLabel(), pb_submit)
            fl_right.addRow(QLabel())
            return wd_ip

        elif index == 1:
            # Iλ 测试对应界面
            wd_i_lambda = QWidget()
            hl_i_lambda = QHBoxLayout(wd_i_lambda)
            hl_i_lambda.setSpacing(50)
            wd_left = QWidget()
            fl_left = QFormLayout(wd_left)
            fl_left.setHorizontalSpacing(25)
            fl_left.setVerticalSpacing(35)
            wd_right = QWidget()
            fl_right = QFormLayout(wd_right)
            fl_right.setHorizontalSpacing(25)
            fl_right.setVerticalSpacing(35)
            hl_i_lambda.addWidget(wd_left)
            hl_i_lambda.addWidget(wd_right)
            lb_u2722a = QLabel("U2722A Settings")

            lb_voltage_value = QLabel("Voltage Value：")
            wd_voltage_value = QWidget()
            hl_voltage_value = QHBoxLayout(wd_voltage_value)
            hl_voltage_value.setContentsMargins(0, 0, 0, 0)
            le_voltage_value = QLineEdit()
            le_voltage_value.setFixedWidth(100)
            cb_voltage_value = QComboBox()
            cb_voltage_value.setFixedWidth(60)
            cb_voltage_value.addItems(["mV", "V"])
            hl_voltage_value.addWidget(le_voltage_value, alignment=Qt.AlignLeft)
            hl_voltage_value.addWidget(cb_voltage_value)

            lb_voltage_range = QLabel("Voltage Range：")
            cb_voltage_range = QComboBox()
            cb_voltage_range.setFixedWidth(100)
            cb_voltage_range.addItems(["2V", "20V"])

            lb_current_nplc = QLabel("Current Nplc：")
            le_current_nplc = QLineEdit()
            le_current_nplc.setFixedWidth(100)

            lb_current_range = QLabel("Current Range：")
            cb_current_range = QComboBox()
            cb_current_range.setFixedWidth(100)
            cb_current_range.addItems(["R1uA", "R10uA", "R100uA", "R1mA", "R10mA", "R120mA", ])

            lb_current_limit = QLabel("Current Limit：")
            wd_current_limit = QWidget()
            hl_current_limit = QHBoxLayout(wd_current_limit)
            hl_current_limit.setContentsMargins(0, 0, 0, 0)
            le_current_limit = QLineEdit()
            le_current_limit.setFixedWidth(100)
            cb_current_limit = QComboBox()
            cb_current_limit.setFixedWidth(60)
            cb_current_limit.addItems(["uA", "mA", "A"])
            hl_current_limit.addWidget(le_current_limit, alignment=Qt.AlignLeft)
            hl_current_limit.addWidget(cb_current_limit)

            lb_output_state = QLabel("Output State：")
            pb_switch = QSwitchButton()

            lb_channel_num = QLabel("Channel Number：")
            cb_channel_num = QComboBox()
            cb_channel_num.setFixedWidth(130)
            cb_channel_num.addItems(
                ["Channel 1", "Channel 2", "Channel 3", "Channel 4", "Channel 5", "Channel 6", "Channel 7", "Channel 8",
                 "Channel 9", "Channel 10", ])

            lb_l8164b = QLabel("L8164B Settings")

            lb_power_value = QLabel("Power Value：")
            le_power_value = QLineEdit()
            le_power_value.setFixedWidth(100)

            lb_wave_point = QLabel("Wave Point：")
            le_wave_point = QLineEdit()
            le_wave_point.setFixedWidth(100)

            wd_blank = QWidget()
            hl_blank = QHBoxLayout(wd_blank)
            hl_blank.setContentsMargins(0, 0, 0, 0)
            lb_blank = QLabel("")
            hl_blank.addWidget(lb_blank)

            wd_blank_2 = QWidget()
            hl_blank_2 = QHBoxLayout(wd_blank_2)
            # hl_blank_2.setContentsMargins(0, 0, 0, 0)
            lb_blank_2 = QLabel("")
            hl_blank_2.addWidget(lb_blank_2)

            pb_submit = QPushButton("保存设置")
            pb_submit.clicked.connect(self.save_i_lambda_setting)
            pb_submit.setFixedWidth(100)

            fl_left.addRow(lb_u2722a)
            fl_left.addRow(lb_voltage_value, wd_voltage_value)
            fl_left.addRow(lb_voltage_range, cb_voltage_range)
            fl_left.addRow(lb_current_nplc, le_current_nplc)
            fl_left.addRow(lb_current_range, cb_current_range)
            fl_left.addRow(lb_current_limit, wd_current_limit)
            fl_left.addRow(lb_output_state, pb_switch)
            fl_right.addRow(lb_l8164b)
            fl_right.addRow(lb_power_value, le_power_value)
            fl_right.addRow(lb_wave_point, le_wave_point)
            fl_right.addRow(QLabel(), wd_blank)
            fl_right.addRow(QLabel(), wd_blank_2)
            fl_right.addRow(lb_channel_num, cb_channel_num)
            fl_right.addRow(QLabel(), pb_submit)
            fl_right.addRow(QLabel())

            return wd_i_lambda

        elif index == 2:
            # IV 测试对应界面
            wd_iv = QWidget()
            hl_iv = QHBoxLayout(wd_iv)
            hl_iv.setSpacing(50)
            wd_left = QWidget()
            fl_left = QFormLayout(wd_left)
            fl_left.setHorizontalSpacing(25)
            fl_left.setVerticalSpacing(35)
            wd_right = QWidget()
            fl_right = QFormLayout(wd_right)
            fl_right.setHorizontalSpacing(25)
            fl_right.setVerticalSpacing(35)
            hl_iv.addWidget(wd_left)
            hl_iv.addWidget(wd_right)
            lb_u2722a = QLabel("U2722A Settings")

            lb_start_voltage = QLabel("Start Voltage：")
            wd_start_voltage = QWidget()
            hl_start_voltage = QHBoxLayout(wd_start_voltage)
            hl_start_voltage.setContentsMargins(0, 0, 0, 0)
            le_start_voltage = QLineEdit()
            le_start_voltage.setFixedWidth(100)
            cb_start_voltage = QComboBox()
            cb_start_voltage.setFixedWidth(60)
            cb_start_voltage.addItems(["mV", "V"])
            hl_start_voltage.addWidget(le_start_voltage, alignment=Qt.AlignLeft)
            hl_start_voltage.addWidget(cb_start_voltage)

            lb_stop_voltage = QLabel("Stop Voltage：")
            wd_stop_voltage = QWidget()
            hl_stop_voltage = QHBoxLayout(wd_stop_voltage)
            hl_stop_voltage.setContentsMargins(0, 0, 0, 0)
            le_stop_voltage = QLineEdit()
            le_stop_voltage.setFixedWidth(100)
            cb_stop_voltage = QComboBox()
            cb_stop_voltage.setFixedWidth(60)
            cb_stop_voltage.addItems(["mV", "V"])
            hl_stop_voltage.addWidget(le_stop_voltage, alignment=Qt.AlignLeft)
            hl_stop_voltage.addWidget(cb_stop_voltage)

            lb_voltage_point = QLabel("Voltage Point：")
            le_voltage_point = QLineEdit()
            le_voltage_point.setFixedWidth(100)

            lb_current_nplc = QLabel("Current Nplc：")
            le_current_nplc = QLineEdit()
            le_current_nplc.setFixedWidth(100)

            lb_current_range = QLabel("Current Range：")
            cb_current_range = QComboBox()
            cb_current_range.setFixedWidth(100)
            cb_current_range.addItems(["R1uA", "R10uA", "R100uA", "R1mA", "R10mA", "R120mA", ])

            lb_current_limit = QLabel("Current Limit：")
            wd_current_limit = QWidget()
            hl_current_limit = QHBoxLayout(wd_current_limit)
            hl_current_limit.setContentsMargins(0, 0, 0, 0)
            le_current_limit = QLineEdit()
            le_current_limit.setFixedWidth(100)
            cb_current_limit = QComboBox()
            cb_current_limit.setFixedWidth(60)
            cb_current_limit.addItems(["uA", "mA", "A"])
            hl_current_limit.addWidget(le_current_limit, alignment=Qt.AlignLeft)
            hl_current_limit.addWidget(cb_current_limit)

            lb_channel_num = QLabel("Channel Number：")
            cb_channel_num = QComboBox()
            cb_channel_num.setFixedWidth(130)
            cb_channel_num.addItems(
                ["Channel 1", "Channel 2", "Channel 3", "Channel 4", "Channel 5", "Channel 6", "Channel 7", "Channel 8",
                 "Channel 9", "Channel 10", ])

            lb_l8164b = QLabel("L8164B Settings")

            lb_wave_len = QLabel("Wave Length：")
            wd_wave_len = QWidget()
            hl_wave_len = QHBoxLayout(wd_wave_len)
            hl_wave_len.setContentsMargins(0, 0, 0, 0)
            le_wave_len = QLineEdit()
            le_wave_len.setFixedWidth(100)
            cb_wave_len = QComboBox()
            cb_wave_len.setFixedWidth(60)
            cb_wave_len.addItem("nm")
            hl_wave_len.addWidget(le_wave_len, alignment=Qt.AlignLeft)
            hl_wave_len.addWidget(cb_wave_len)

            lb_power_unit = QLabel("Power Unit：")
            cb_power_unit = QComboBox()
            cb_power_unit.setFixedWidth(100)
            cb_power_unit.addItems(["dBm", "W"])

            lb_power_value = QLabel("Power Value：")
            le_power_value = QLineEdit()
            le_power_value.setFixedWidth(100)

            wd_blank = QWidget()
            hl_blank = QHBoxLayout(wd_blank)
            hl_blank.setContentsMargins(0, 0, 0, 0)
            lb_blank = QLabel("")
            hl_blank.addWidget(lb_blank)

            pb_submit = QPushButton("保存设置")
            pb_submit.clicked.connect(self.save_iv_setting)
            pb_submit.setFixedWidth(100)

            fl_left.addRow(lb_u2722a)
            fl_left.addRow(lb_start_voltage, wd_start_voltage)
            fl_left.addRow(lb_stop_voltage, wd_stop_voltage)
            fl_left.addRow(lb_voltage_point, le_voltage_point)
            fl_left.addRow(lb_current_nplc, le_current_nplc)
            fl_left.addRow(lb_current_range, cb_current_range)
            fl_left.addRow(lb_current_limit, wd_current_limit)
            fl_right.addRow(lb_l8164b)
            fl_right.addRow(lb_wave_len, wd_wave_len)
            fl_right.addRow(lb_power_unit, cb_power_unit)
            fl_right.addRow(lb_power_value, le_power_value)
            fl_right.addRow(QLabel(), wd_blank)
            fl_right.addRow(lb_channel_num, cb_channel_num)
            fl_right.addRow(QLabel(), pb_submit)
            fl_right.addRow(QLabel())
            return wd_iv

        elif index == 3:
            # PV 测试对应界面
            wd_pv = QWidget()
            hl_pv = QHBoxLayout(wd_pv)
            hl_pv.setSpacing(50)
            wd_left = QWidget()
            fl_left = QFormLayout(wd_left)
            fl_left.setHorizontalSpacing(25)
            fl_left.setVerticalSpacing(35)
            wd_right = QWidget()
            fl_right = QFormLayout(wd_right)
            fl_right.setHorizontalSpacing(25)
            fl_right.setVerticalSpacing(35)
            hl_pv.addWidget(wd_left)
            hl_pv.addWidget(wd_right)
            lb_u2722a = QLabel("U2722A Settings")

            lb_start_voltage = QLabel("Start Voltage：")
            wd_start_voltage = QWidget()
            hl_start_voltage = QHBoxLayout(wd_start_voltage)
            hl_start_voltage.setContentsMargins(0, 0, 0, 0)
            le_start_voltage = QLineEdit()
            le_start_voltage.setFixedWidth(100)
            cb_start_voltage = QComboBox()
            cb_start_voltage.setFixedWidth(60)
            cb_start_voltage.addItems(["mV", "V"])
            hl_start_voltage.addWidget(le_start_voltage, alignment=Qt.AlignLeft)
            hl_start_voltage.addWidget(cb_start_voltage)

            lb_stop_voltage = QLabel("Stop Voltage：")
            wd_stop_voltage = QWidget()
            hl_stop_voltage = QHBoxLayout(wd_stop_voltage)
            hl_stop_voltage.setContentsMargins(0, 0, 0, 0)
            le_stop_voltage = QLineEdit()
            le_stop_voltage.setFixedWidth(100)
            cb_stop_voltage = QComboBox()
            cb_stop_voltage.setFixedWidth(60)
            cb_stop_voltage.addItems(["mV", "V"])
            hl_stop_voltage.addWidget(le_stop_voltage, alignment=Qt.AlignLeft)
            hl_stop_voltage.addWidget(cb_stop_voltage)

            lb_voltage_point = QLabel("Voltage Point：")
            le_voltage_point = QLineEdit()
            le_voltage_point.setFixedWidth(100)

            lb_current_nplc = QLabel("Current Nplc：")
            le_current_nplc = QLineEdit()
            le_current_nplc.setFixedWidth(100)

            lb_current_range = QLabel("Current Range：")
            cb_current_range = QComboBox()
            cb_current_range.setFixedWidth(100)
            cb_current_range.addItems(["R1uA", "R10uA", "R100uA", "R1mA", "R10mA", "R120mA", ])

            lb_current_limit = QLabel("Current Limit：")
            wd_current_limit = QWidget()
            hl_current_limit = QHBoxLayout(wd_current_limit)
            hl_current_limit.setContentsMargins(0, 0, 0, 0)
            le_current_limit = QLineEdit()
            le_current_limit.setFixedWidth(100)
            cb_current_limit = QComboBox()
            cb_current_limit.setFixedWidth(60)
            cb_current_limit.addItems(["uA", "mA", "A"])
            hl_current_limit.addWidget(le_current_limit, alignment=Qt.AlignLeft)
            hl_current_limit.addWidget(cb_current_limit)

            lb_channel_num = QLabel("Channel Number：")
            cb_channel_num = QComboBox()
            cb_channel_num.setFixedWidth(130)
            cb_channel_num.addItems(
                ["Channel 1", "Channel 2", "Channel 3", "Channel 4", "Channel 5", "Channel 6", "Channel 7", "Channel 8",
                 "Channel 9", "Channel 10", ])

            lb_l8164b = QLabel("L8164B Settings")

            lb_wave_len = QLabel("Wave Length：")
            wd_wave_len = QWidget()
            hl_wave_len = QHBoxLayout(wd_wave_len)
            hl_wave_len.setContentsMargins(0, 0, 0, 0)
            le_wave_len = QLineEdit()
            le_wave_len.setFixedWidth(100)
            cb_wave_len = QComboBox()
            cb_wave_len.setFixedWidth(60)
            cb_wave_len.addItem("nm")
            hl_wave_len.addWidget(le_wave_len, alignment=Qt.AlignLeft)
            hl_wave_len.addWidget(cb_wave_len)

            lb_power_unit = QLabel("Power Unit：")
            cb_power_unit = QComboBox()
            cb_power_unit.setFixedWidth(100)
            cb_power_unit.addItems(["dBm", "W"])

            lb_power_value = QLabel("Power Value：")
            le_power_value = QLineEdit()
            le_power_value.setFixedWidth(100)

            wd_blank = QWidget()
            hl_blank = QHBoxLayout(wd_blank)
            hl_blank.setContentsMargins(0, 0, 0, 0)
            lb_blank = QLabel("")
            hl_blank.addWidget(lb_blank)

            pb_submit = QPushButton("保存设置")
            pb_submit.clicked.connect(self.save_pv_setting)
            pb_submit.setFixedWidth(100)

            fl_left.addRow(lb_u2722a)
            fl_left.addRow(lb_start_voltage, wd_start_voltage)
            fl_left.addRow(lb_stop_voltage, wd_stop_voltage)
            fl_left.addRow(lb_voltage_point, le_voltage_point)
            fl_left.addRow(lb_current_nplc, le_current_nplc)
            fl_left.addRow(lb_current_range, cb_current_range)
            fl_left.addRow(lb_current_limit, wd_current_limit)
            fl_right.addRow(lb_l8164b)
            fl_right.addRow(lb_wave_len, wd_wave_len)
            fl_right.addRow(lb_power_unit, cb_power_unit)
            fl_right.addRow(lb_power_value, le_power_value)
            fl_right.addRow(QLabel(), wd_blank)
            fl_right.addRow(lb_channel_num, cb_channel_num)
            fl_right.addRow(QLabel(), pb_submit)
            fl_right.addRow(QLabel())
            return wd_pv

        elif index == 4:
            # OE_TE 测试对应界面
            wd_oe_te = QWidget()
            hl_oe_te = QHBoxLayout(wd_oe_te)
            hl_oe_te.setSpacing(50)
            wd_left = QWidget()
            fl_left = QFormLayout(wd_left)
            fl_left.setHorizontalSpacing(25)
            fl_left.setVerticalSpacing(35)
            wd_right = QWidget()
            fl_right = QFormLayout(wd_right)
            fl_right.setHorizontalSpacing(25)
            fl_right.setVerticalSpacing(35)
            hl_oe_te.addWidget(wd_left)
            hl_oe_te.addWidget(wd_right)
            lb_u2722a = QLabel("U2722A Settings")

            lb_voltage_value = QLabel("Voltage Value：")
            wd_voltage_value = QWidget()
            hl_voltage_value = QHBoxLayout(wd_voltage_value)
            hl_voltage_value.setContentsMargins(0, 0, 0, 0)
            le_voltage_value = QLineEdit()
            le_voltage_value.setFixedWidth(100)
            cb_voltage_value = QComboBox()
            cb_voltage_value.setFixedWidth(60)
            cb_voltage_value.addItems(["mV", "V"])
            hl_voltage_value.addWidget(le_voltage_value, alignment=Qt.AlignLeft)
            hl_voltage_value.addWidget(cb_voltage_value)

            lb_voltage_range = QLabel("Voltage Range：")
            cb_voltage_range = QComboBox()
            cb_voltage_range.setFixedWidth(100)
            cb_voltage_range.addItems(["2V", "20V"])

            lb_current_nplc = QLabel("Current Nplc：")
            le_current_nplc = QLineEdit()
            le_current_nplc.setFixedWidth(100)

            lb_current_range = QLabel("Current Range：")
            cb_current_range = QComboBox()
            cb_current_range.setFixedWidth(100)
            cb_current_range.addItems(["R1uA", "R10uA", "R100uA", "R1mA", "R10mA", "R120mA", ])

            lb_current_limit = QLabel("Current Limit：")
            wd_current_limit = QWidget()
            hl_current_limit = QHBoxLayout(wd_current_limit)
            hl_current_limit.setContentsMargins(0, 0, 0, 0)
            le_current_limit = QLineEdit()
            le_current_limit.setFixedWidth(100)
            cb_current_limit = QComboBox()
            cb_current_limit.setFixedWidth(60)
            cb_current_limit.addItems(["uA", "mA", "A"])
            hl_current_limit.addWidget(le_current_limit, alignment=Qt.AlignLeft)
            hl_current_limit.addWidget(cb_current_limit)

            lb_output_state = QLabel("Output State：")
            pb_switch = QSwitchButton()

            lb_channel_num = QLabel("Channel Number：")
            cb_channel_num = QComboBox()
            cb_channel_num.setFixedWidth(130)
            cb_channel_num.addItems(
                ["Channel 1", "Channel 2", "Channel 3", "Channel 4", "Channel 5", "Channel 6", "Channel 7", "Channel 8",
                 "Channel 9", "Channel 10", ])

            lb_l8164b = QLabel("L8164B Settings")

            lb_wave_len = QLabel("Wave Length：")
            wd_wave_len = QWidget()
            hl_wave_len = QHBoxLayout(wd_wave_len)
            hl_wave_len.setContentsMargins(0, 0, 0, 0)
            le_wave_len = QLineEdit()
            le_wave_len.setFixedWidth(100)
            cb_wave_len = QComboBox()
            cb_wave_len.setFixedWidth(60)
            cb_wave_len.addItem("nm")
            hl_wave_len.addWidget(le_wave_len, alignment=Qt.AlignLeft)
            hl_wave_len.addWidget(cb_wave_len)

            lb_power_unit = QLabel("Power Unit：")
            cb_power_unit = QComboBox()
            cb_power_unit.setFixedWidth(100)
            cb_power_unit.addItems(["dBm", "W"])

            lb_power_value = QLabel("Power Value：")
            le_power_value = QLineEdit()
            le_power_value.setFixedWidth(100)

            lb_stable_schedule = QLabel("Stable Schedule：")
            le_stable_schedule = QLineEdit()
            le_stable_schedule.setFixedWidth(100)

            wd_blank = QWidget()
            hl_blank = QHBoxLayout(wd_blank)
            hl_blank.setContentsMargins(0, 0, 0, 0)
            lb_blank = QLabel("")
            hl_blank.addWidget(lb_blank)

            pb_submit = QPushButton("保存设置")
            pb_submit.clicked.connect(self.save_oe_te_setting)
            pb_submit.setFixedWidth(100)

            fl_left.addRow(lb_u2722a)
            fl_left.addRow(lb_voltage_value, wd_voltage_value)
            fl_left.addRow(lb_voltage_range, cb_voltage_range)
            fl_left.addRow(lb_current_nplc, le_current_nplc)
            fl_left.addRow(lb_current_range, cb_current_range)
            fl_left.addRow(lb_current_limit, wd_current_limit)
            fl_left.addRow(lb_output_state, pb_switch)
            fl_right.addRow(lb_l8164b)
            fl_right.addRow(lb_wave_len, wd_wave_len)
            fl_right.addRow(lb_power_unit, cb_power_unit)
            fl_right.addRow(lb_power_value, le_power_value)
            fl_right.addRow(lb_stable_schedule, le_stable_schedule)
            fl_right.addRow(lb_channel_num, cb_channel_num)
            fl_right.addRow(QLabel(), pb_submit)
            fl_right.addRow(QLabel())
            return wd_oe_te

        elif index == 5:
            # OO 测试对应界面
            wd_oo = QWidget()

            return wd_oo

        elif index == 6:
            # OO_TE 测试对应界面
            wd_oo_te = QWidget()

            return wd_oo_te

        elif index == 7:
            # SPEC SCAN 测试对应界面
            wd_spec_scan = QWidget()

            return wd_spec_scan

    # TEST MODE 对应界面动态框架
    def _show_test_mode_wd(self):
        sender = self.sender()
        sub_die_name = sender.parent().children()[1].currentText().strip()  # 名称
        self.test_mode_index = sender.parent().children()[2].currentIndex()  # 模式索引
        test_mode_name = sender.parent().children()[2].currentText()  # 模式名称
        self.current_sub_die_info = self.all_sub_die_info_dict[sub_die_name]
        self.dia_test_mode = QDialog()
        with open("./static/style_sheet/origin.qss", mode='r', encoding='utf-8') as f:
            style_sheet = f.read()
        self.dia_test_mode.setStyleSheet(style_sheet)
        self.dia_test_mode.setWindowTitle(sub_die_name + "  " + test_mode_name + "  TEST  SETTING")
        self.dia_test_mode.setWindowIcon(QIcon("./static/style_sheet/icons/logo.png"))
        self.dia_test_mode.resize(200, 200)
        self.vl_test_mode = QVBoxLayout(self.dia_test_mode)
        self.vl_test_mode.setContentsMargins(0, 0, 0, 0)
        # 界面构建 在这里接收一个对应的 QWidget 控件，加到自己的框中  current_sub_die_info 中保存有相应信息
        self.wd_test_mode = self.reflect_test_widget(self.test_mode_index)
        self.vl_test_mode.addWidget(self.wd_test_mode)
        self.dia_test_mode.exec()

    # 更新字典中所保存的对应测试模式信息
    def update_test_setting(self):
        pass

    # DCP 对应界面
    def _show_dcp_wd(self):
        self.dia_dcp = DCPDialog()
        self.dia_dcp.send_signal.connect(self.update_dcp_info)
        self.dia_dcp.exec()

    # 更新 DCP 探针设置
    def update_dcp_info(self, value):
        self.dia_dcp.close()
        new_cb_index_list = []
        for cb in self.dia_dcp.children()[1].children()[2:-1:2]:
            new_cb_index_list.append(cb.currentIndex())
        if self.check_dcp_valid(new_cb_index_list):
            # self.current_sub_die_info[1] = new_cb_index_list
            self.show_info("应用成功！")
        else:
            self.show_critical("SMU 设置不合法，请重试！")

    def save_file(self):
        self.show_info("save_file")

    def read_config(self):
        self.show_info("read_config")

    def show_info(self, content):
        QMessageBox.information(self, "提示", content)

    def show_critical(self, content):
        QMessageBox.critical(self, "警告", content)

    # 判断 DCP 定义合法性
    @staticmethod
    def check_dcp_valid(dcp_list):
        new_list = []
        for i in dcp_list:
            if i != 0:
                # None 不判断，其他的都加到新列表中
                new_list.append(i)
        # 该列表中没有重复元素，说明无重复的DCP
        if len(new_list) == len(set(new_list)):
            return True
        return False

    def save_ip_setting(self):
        sender = self.sender()
        widget = sender.parent().parent()
        reply = QMessageBox.question(widget, '提示', "确认保存配置？", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            self.current_sub_die_info[0][self.test_mode_index]["new"] = '1'
            pass
        else:
            pass

    def save_i_lambda_setting(self):
        sender = self.sender()
        widget = sender.parent().parent()
        reply = QMessageBox.question(widget, '提示', "确认保存配置？", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            self.current_sub_die_info[0][self.test_mode_index]["new"] = '1'
        else:
            pass

    def save_iv_setting(self):
        sender = self.sender()
        widget = sender.parent().parent()
        reply = QMessageBox.question(widget, '提示', "确认保存配置？", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            # 保存在 self.current_sub_die_info[0][self.test_mode_index] 中
            pass
        else:
            pass

    def save_pv_setting(self):
        sender = self.sender()
        widget = sender.parent().parent()
        reply = QMessageBox.question(widget, '提示', "确认保存配置？", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            # 保存在 self.current_sub_die_info[0][self.test_mode_index] 中
            pass
        else:
            pass

    def save_oe_te_setting(self):
        sender = self.sender()
        widget = sender.parent().parent()
        reply = QMessageBox.question(widget, '提示', "确认保存配置？", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            # 保存在 self.current_sub_die_info[0][self.test_mode_index] 中
            pass
        else:
            pass


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Driver()
    window.show()
    sys.exit(app.exec())
